package org.misalen.web.server.controllers.user;

import org.misalen.annotations.ControllerApi;
import org.misalen.annotations.InterfaceApi;
import org.misalen.web.database.model.mysql.pro.ProWebAdvertisingCarrier;
import org.misalen.web.database.util.PaginationFrom;
import org.misalen.web.server.config.ServerResult;
import org.misalen.web.server.config.ServerUrl;
import org.misalen.web.server.controllers.BaseController;
import org.misalen.web.server.exception.CustomerException;
import org.misalen.web.server.forms.user.CarrierHotForm;
import org.misalen.web.server.forms.user.CarrierListForm;
import org.misalen.web.server.util.FormUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@ControllerApi(value = "载体文章", description = "", index = 300)
@RequestMapping(value = ServerUrl.UserCarrierControllerUrl)
public class UserCarrierController extends BaseController {

	@RequestMapping(value = "/hot", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "热文", resultClass = { PaginationFrom.class, ProWebAdvertisingCarrier.class })
	public @ResponseBody ServerResult hot(@RequestBody CarrierHotForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProWebAdvertisingCarrier> pagination = formToPage(form, ProWebAdvertisingCarrier.class);
		return ServerResult.ok(proWebAdvertisingCarrierRepository.findByType(pagination, "热文"));
	}

	@RequestMapping(value = "/list", method = { RequestMethod.POST }, produces = "application/json")
	@InterfaceApi(value = "文章列表", resultClass = { PaginationFrom.class, ProWebAdvertisingCarrier.class })
	public @ResponseBody ServerResult list(@RequestBody CarrierListForm form) throws CustomerException {
		FormUtil.validate(form);
		PaginationFrom<ProWebAdvertisingCarrier> pagination = formToPage(form, ProWebAdvertisingCarrier.class);
		return ServerResult.ok(proWebAdvertisingCarrierRepository.findByType(pagination, form.getType()));
	}
}
